<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$link = mysql_connect("localhost", "root", "ornitorrinco666") or die(mysql_error());
mysql_select_db("pba", $link) or die(mysql_error());


$sql="SELECT a.*,b.property_id ,b.id as poll_id FROM candidates a LEFT JOIN  family_polls b on a.family_poll_id=b.id;";

$res=  mysql_query($sql, $link)or die(mysql_error());

while ($row = mysql_fetch_array($res)) {
    $genero="";
    
    
    if($row['genero']=="Hombre"){
        $genero="Masculino";
    }else{
        $genero="Femenino";
    }
    
    
    $sql1="INSERT INTO `beneficiaries` (  `nombres`, `genero`, `beneficiary_id`, `property_id`, `estado_civil`, `escolaridad`, `seguridad_social`, `ocupacion`, `nivel_sisben`, `edad`, `prestadora_salud`, `discapacidad`,calificacion_visita) VALUES";
    $sql1.=" ('".$row['primer_nombre']."','$genero',0,".$row['property_id'].",'".$row['estado_civil']."','".$row['escolaridad']."','".$row['seguridad_social']."','".$row['ocupacion']."','".$row['nivel_sisben']."','".$row['edad']."','".$row['prestadora_salud']."','".$row['discapacidad']."','Cumple');";
       
    mysql_query($sql1, $link)or die(mysql_error());
    
    $idAnterior=$row['id'];
    $idNuevo =  mysql_insert_id($link);
    $sql2="UPDATE family_polls set beneficiary_id=$idNuevo WHERE id=".$row['poll_id'];
     mysql_query($sql2, $link)or die(mysql_error());
    $sql3="UPDATE homes  set beneficiary_id=$idNuevo WHERE candidate_id=".$idAnterior;
    mysql_query($sql3, $link)or die(mysql_error());
    $sql4="UPDATE productive_polls  set beneficiary_id=$idNuevo WHERE candidate_id=".$idAnterior;
    mysql_query($sql4, $link)or die(mysql_error());
    $sql4="UPDATE asociations  set beneficiary_id=$idNuevo WHERE candidate_id=".$idAnterior;
    mysql_query($sql4, $link)or die(mysql_error());
    
    
}
?>


